﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Diagnostics;

namespace MixScriptWeb
{
    public class cBrowserRedirect
    {
        private const int Httpsport = 443;
        private const int HttpPort = 7001;
        public static string HOST_FOLDER = "";



        public static void ShowMsgMemberSessionFail(Page pg)
        {

            ShowMsgAtBlock(pg,
                           "Error: can't read member session",
                           "errMemberSess");
        }

        public static void ShowMsgValidationFail(Page pg)
        {
            ShowMsgAtBlock(pg,
                           "Validation Failed.\\nPlease check all fields marked with red cycle",
                           "errValidNotify");
        }

        public static void ShowMsgValidationColBorFail(Page pg)
        {
            ShowMsgAtBlock(pg,
                           "Validation Failed.\\nPlease select settlement of collateral borrowing !",
                           "errValidNotify");
        }

        public static string RedirectedPage(Page pg, bool toHttps, string toLocation)
        {
            Uri u = pg.Request.Url;
            string uri = string.Empty;
            if (toHttps)
            {
                int nPort = Httpsport;// CCommon.HttpsPort;
                if ((nPort < 1) || (nPort == 443))
                {
                    uri = string.Format("https://{0}/{1}/{2}", u.Host, HOST_FOLDER, toLocation);
                }
                else
                {
                    uri = string.Format("https://{0}:{1}/{2}/{3}", u.Host, nPort, HOST_FOLDER, toLocation);
                }
            }
            else
            {
                int nPort = HttpPort;//CCommon.HttpPort;
                if ((nPort < 1) || (nPort == 80))
                {
                    uri = string.Format("http://{0}/{1}/{2}", u.Host, HOST_FOLDER, toLocation);
                }
                else
                {
                    uri = string.Format("http://{0}:{1}/{2}/{3}", u.Host, nPort, HOST_FOLDER, toLocation);
                }
            }

            ShowScriptAtBlock(pg,
                              string.Format("javascript:location.replace('{0}');", uri), "httpsRedir");

            return uri;
        }

        public static int GetIntApplication(HttpApplicationState app, string appName)
        {
            int n = 0;

            try
            {
                n = (int)app[appName];
                if (n == 0)
                    n = 0;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(
                  string.Format("CBrowserRedirect:GetIntApplication - {0}", ex.Message));

                n = 0;
            }

            return n;
        }

        public static string GetStringApplication(HttpApplicationState app, string appName)
        {
            string tmp = string.Empty;

            try
            {
                tmp = (string)app[appName];
            }
            catch (Exception ex)
            {
                Debug.WriteLine(
                  string.Format("CBrowserRedirect:GetStringApplication - {0}", ex.Message));

                tmp = string.Empty;
            }

            return tmp;
        }

        public static object GetObjectSession(HttpSessionState sess, string sessName, bool removeIt)
        {
            object o = null;

            try
            {
                o = sess[sessName];
                if (removeIt)
                {
                    try
                    {
                        sess.Remove(sessName);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(
                          string.Format("CBrowserRedirect:GetObjectSession - {0}", ex.Message));
                    }
                }
            }
            catch
            {
                o = null;
            }

            return o;
        }

        public static object GetObjectSession(HttpSessionState sess, string sessName)
        {
            return GetObjectSession(sess, sessName, false);
        }

        public static bool GetBooleanSession(HttpSessionState sess, string sessName)
        {
            return GetBooleanSession(sess, sessName, false);
        }

        public static bool GetBooleanSession(HttpSessionState sess, string sessName, bool removeIt)
        {
            bool bl = false;

            try
            {
                //bl = (bool)sess[sessName];
                bool.TryParse((sess[sessName]).ToString(), out bl);
                if (removeIt)
                {
                    try
                    {
                        sess.Remove(sessName);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(
                          string.Format("CBrowserRedirect:GetBooleanSession - {0}", ex.Message));
                    }
                }
            }
            catch
            {
                bl = false;
            }

            return bl;
        }

        public static string GetStringSession(HttpSessionState sess, string sessName, bool removeIt)
        {
            if (sess == null)
                return string.Empty;

            string s = string.Empty;

            try
            {
                s = (string)sess[sessName];
                if (removeIt)
                {
                    try
                    {
                        sess.Remove(sessName);
                    }
                    finally
                    {
                        ;
                    }
                }
                if (s == null)
                    s = string.Empty;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(
                  string.Format("CBrowserRedirect:GetStringSession - {0}", ex.Message));

                s = string.Empty;
            }

            return s.Trim();
        }

        public static string GetStringSession(HttpSessionState sess, string sessName)
        {
            return GetStringSession(sess, sessName, false);
        }

        public static int GetIntSession(HttpSessionState sess, string sessName, bool removeIt)
        {
            int n = 0;

            try
            {
                n = (int)sess[sessName];
                if (removeIt)
                {
                    try
                    {
                        sess.Remove(sessName);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(
                          string.Format("CBrowserRedirect:GetIntSession - {0}", ex.Message));
                    }
                }
            }
            catch
            {
                n = 0;
            }

            return n;
        }

        public static int GetIntSession(HttpSessionState sess, string sessName)
        {
            return GetIntSession(sess, sessName, false);
        }

        public static void ShowMsgAtStartup(Page pg, string msg, string key)
        {
            if (pg == null)
                return;

            ClientScriptManager csm = pg.ClientScript;
            if (!csm.IsStartupScriptRegistered(pg.GetType(), key))
            {
                msg = msg.Replace("\"", "\\\"");
                msg = msg.Replace("\n", "\\n");
                msg = msg.Replace("\r", "");
                string sMsg = string.Format("alert(\"{0}\");", msg);
                //csm.RegisterStartupScript(pg.GetType(), key, sMsg, true);
                ScriptManager.RegisterStartupScript(pg, pg.GetType(), key, sMsg, true);
            }
        }

        public static void ShowMsgAtBlock(Page pg, string msg, string key)
        {
            if (pg == null)
                return;

            ClientScriptManager csm = pg.ClientScript;
            if (!csm.IsClientScriptBlockRegistered(pg.GetType(), key))
            {
                msg = msg.Replace("\"", "\\\"");
                msg = msg.Replace("\n", "\\n");
                msg = msg.Replace("\r", "");
                string sMsg = string.Format("alert(\"{0}\");", msg);
                //csm.RegisterClientScriptBlock(pg.GetType(), key, sMsg, true);
                ScriptManager.RegisterClientScriptBlock(pg, pg.GetType(), key, sMsg, true);
            }
        }

        public static void ShowScriptAtStartup(Page pg, string msg, string key)
        {
            if (pg == null)
                return;

            ClientScriptManager csm = pg.ClientScript;
            if (!csm.IsStartupScriptRegistered(pg.GetType(), key))
            {
                //csm.RegisterStartupScript(pg.GetType(), key, msg, true);
                ScriptManager.RegisterStartupScript(pg, pg.GetType(), key, msg, true);
            }
        }

        public static void ShowScriptAtBlock(Page pg, string msg, string key)
        {
            if (pg == null)
                return;

            ClientScriptManager csm = pg.ClientScript;
            if (!csm.IsClientScriptBlockRegistered(pg.GetType(), key))
            {
                //csm.RegisterClientScriptBlock(pg.GetType(), key, msg, true);
                ScriptManager.RegisterClientScriptBlock(pg, pg.GetType(), key, msg, true);
            }
        }

        public static void ReplacePage(Page pg, string uriLocation)
        {
            if (pg == null)
                return;

            string sLocJs = string.Format("location.replace('{0}')", uriLocation);
            ShowScriptAtBlock(pg, sLocJs, "replcPage");
        }

        public static void SetForbiddenToThisPage(Page pg)
        {
            if (pg == null)
                return;

            if (pg.Master != null)
            {
                pg.Master.Response.StatusCode = 406;
            }

            pg.Response.StatusCode = 406;
            pg.Response.Flush();
            pg.Response.End();
        }

        public static void SetOfflineToThisSite(Page pg)
        {
            if (pg == null)
                return;

            HttpResponse resp = pg.Response;

            resp.Redirect("~/system/E-Message/e001.html", true);
        }

        public static void SetOfflineToThisSite(HttpResponse resp)
        {
            if (resp == null)
                return;

            resp.Redirect("~/system/E-Message/e001.html", true);
        }

        public static void SetOfflineToThisSiteInstallation(Page pg)
        {
            if (pg == null)
                return;

            HttpResponse resp = pg.Response;

            resp.Redirect("~/system/E-Message/e002.html", true);
        }

        public static void SetOfflineToThisSiteInstallation(HttpResponse resp)
        {
            if (resp == null)
                return;

            resp.Redirect("~/system/E-Message/e002.html", true);
        }

        public static void SetCustomErrorToThisSite(HttpResponse resp, string errCode)
        {
            if (resp == null)
                return;

            resp.Redirect(
                    string.Format("~/system/E-Message/{0}.html", errCode),
                    true);
        }

        public static void SendBackToMain(Page pg)
        {
            if (pg == null)
                return;

            RedirectedPage(pg, false, "main.html");
        }

        public static void SendBackToIndex(Page pg)
        {
            if (pg == null)
                return;

            RedirectedPage(pg, false, "index.html");
        }
    }
}